package com.test.dao.impl;


import com.test.dao.MerchantDao;
import com.test.domain.Merchant;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Types;
import java.util.List;

@Repository
public class MerchantDaoImpl implements MerchantDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    //用于登录的操作
    @Override
    public int registerAndLogin(int C_M, String phoneNumber, String password) {

        if (phoneNumber=="" || password==""){
            return 3;
        }
        else {
            Object execute_test = jdbcTemplate.execute("execute register_and_login ?,?,?,? ",
                    (CallableStatementCallback) cs -> {
                        cs.setObject("C_M",C_M);
                        cs.setObject("phone_number",phoneNumber);
                        cs.setObject("password",password);
                        cs.setObject("status",-1);
                        cs.registerOutParameter("status", Types.INTEGER);
                        cs.execute();
                        Object ksbm = cs.getObject("status");
                        return ksbm;
                    });
            System.out.println(execute_test);
            return (int)execute_test;
        }




    }

    @Override
    public Merchant getMerchantByPhoneNumber(String phoneNumber) {
        String sql = "select * from Merchant where merchant_phone_number=?";
        Merchant merchant=jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Merchant>(Merchant.class),phoneNumber);
        System.out.println(merchant);
        return merchant;
    }

    @Override
    public void updateShopName(Merchant merchant) {
        String sql = "update Merchant set shop_name=? where merchant_phone_number=?";
        jdbcTemplate.update(sql,merchant.getShopName(),merchant.getMerchantPhoneNumber());
    }

    @Override
    public void updateIsWorking(Merchant merchant) {
        String sql = "update Merchant set is_working=? where merchant_phone_number=?";
        jdbcTemplate.update(sql,merchant.getIsWorking(),merchant.getMerchantPhoneNumber());
    }

    @Override
    public void updateShopDescription(Merchant merchant) {
        String sql = "update Merchant set shop_description=? where merchant_phone_number=?";
        jdbcTemplate.update(sql,merchant.getShopDescription(),merchant.getMerchantPhoneNumber());
    }

    @Override
    public List<Merchant> getMerchantListByKeyword(String keyword) {
        String merchantSqlLike="select * from merchant where shop_name is not null " +
                "and shop_description is not null " +
                "and is_working=1 " +
                "and employee_number is null " +
                "and shop_name like ?";
        return jdbcTemplate.query(merchantSqlLike,new BeanPropertyRowMapper<Merchant>(Merchant.class),
                "%"+keyword+"%");
    }

    @Override
    public List<Merchant> getMerchantListByKeywordNotLike(String keyword) {
        String merchantSqlNotLike="select * from merchant where shop_name is not null " +
                "and shop_description is not null " +
                "and is_working=1 " +
                "and employee_number is null " +
                "and shop_name not like ?";
        return jdbcTemplate.query(merchantSqlNotLike,new BeanPropertyRowMapper<Merchant>(Merchant.class),
                "%"+keyword+"%");
    }
}
